package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.service.OrderService;
import com.draven.mall.service.MerchantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员订单监控控制器
 */
@RestController
@RequestMapping("/api/admin/order")
public class AdminOrderController {

    private static final Logger log = LoggerFactory.getLogger(AdminOrderController.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private MerchantService merchantService;

    /**
     * 获取所有订单（用于实时监控）
     */
    @GetMapping("/all")
    public ApiResponse getAllOrders(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("page", page);
            params.put("size", size);
            
            if (keyword != null && !keyword.trim().isEmpty()) {
                params.put("keyword", keyword.trim());
            }
            
            if (status != null) {
                params.put("status", status);
            }
            
            Map<String, Object> result = orderService.getAllOrders(params);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("获取所有订单失败", e);
            return ApiResponse.error("获取订单数据失败");
        }
    }

    /**
     * 获取订单统计数据
     */
    @GetMapping("/statistics")
    public ApiResponse getOrderStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Integer merchantId) {
        try {
            Map<String, Object> params = new HashMap<>();
            
            if (startDate != null && !startDate.trim().isEmpty()) {
                params.put("startDate", startDate.trim());
            }
            
            if (endDate != null && !endDate.trim().isEmpty()) {
                params.put("endDate", endDate.trim());
            }
            
            if (merchantId != null) {
                params.put("merchantId", merchantId);
            }
            
            Map<String, Object> statistics = orderService.getOrderStatistics(params);
            return ApiResponse.success(statistics);
        } catch (Exception e) {
            log.error("获取订单统计数据失败", e);
            return ApiResponse.error("获取统计数据失败");
        }
    }

    /**
     * 获取订单状态分布统计
     */
    @GetMapping("/status-distribution")
    public ApiResponse getOrderStatusDistribution() {
        try {
            Map<String, Object> distribution = orderService.getOrderStatusDistribution();
            return ApiResponse.success(distribution);
        } catch (Exception e) {
            log.error("获取订单状态分布失败", e);
            return ApiResponse.error("获取订单状态分布失败");
        }
    }

    /**
     * 获取实时订单流量统计
     */
    @GetMapping("/real-time-flow")
    public ApiResponse getOrderRealTimeFlow(
            @RequestParam(required = false, defaultValue = "hour") String granularity) {
        try {
            Map<String, Object> flowData = orderService.getOrderRealTimeFlow(granularity);
            return ApiResponse.success(flowData);
        } catch (Exception e) {
            log.error("获取实时订单流量失败", e);
            return ApiResponse.error("获取实时订单流量失败");
        }
    }

    /**
     * 获取异常订单列表
     */
    @GetMapping("/abnormal")
    public ApiResponse getAbnormalOrders(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        try {
            Map<String, Object> result = orderService.getAbnormalOrders(page, size);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("获取异常订单失败", e);
            return ApiResponse.error("获取异常订单失败");
        }
    }

    /**
     * 获取商家列表（用于下拉选择）
     */
    @GetMapping("/merchants")
    public ApiResponse getMerchants() {
        try {
            List<Map<String, Object>> merchants = merchantService.getAllMerchants();
            return ApiResponse.success(merchants);
        } catch (Exception e) {
            log.error("获取商家列表失败", e);
            return ApiResponse.error("获取商家列表失败");
        }
    }

    /**
     * 获取商家订单排行
     */
    @GetMapping("/merchant-ranking")
    public ApiResponse getMerchantRanking(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            Map<String, Object> params = new HashMap<>();
            
            if (startDate != null && !startDate.trim().isEmpty()) {
                params.put("startDate", startDate.trim());
            }
            
            if (endDate != null && !endDate.trim().isEmpty()) {
                params.put("endDate", endDate.trim());
            }
            
            List<Map<String, Object>> ranking = orderService.getMerchantRanking(params);
            return ApiResponse.success(ranking);
        } catch (Exception e) {
            log.error("获取商家订单排行失败", e);
            return ApiResponse.error("获取商家订单排行失败");
        }
    }
} 